package com.carol.bigdata.fuction

import org.apache.spark.util.AccumulatorV2

import scala.collection.mutable

class MyAccumulator extends AccumulatorV2[Int,Set[Int]]{
    // 定义一个集合，用来装game_id
    var set: mutable.Set[Int] = mutable.Set[Int]()
    
    // 是否为初始化状态，如果集合数据为空，即为初始化状态
    override def isZero: Boolean = set.isEmpty
    
    // 复制累加器
    override def copy(): AccumulatorV2[Int, Set[Int]] = {
        val newAccumulator = new MyAccumulator()
        newAccumulator.set = this.set
        newAccumulator
    }
    
    // 重置累加器。清空集合元素。让集合为空
    override def reset(): Unit = {
        this.set.clear()
    }
    
    override def add(v: Int): Unit = {
        this.set.add(v)
    }
    
    override def merge(other: AccumulatorV2[Int, Set[Int]]): Unit = {
        this.set ++= other.value
    }
    
    override def value: Set[Int] = {
        this.set.toSet
    }
}
